package cn.xyz.mianshi.opensdk;

import cn.xyz.mianshi.model.OfficialInfo;
import cn.xyz.mianshi.model.PageResult;
import cn.xyz.mianshi.utils.SKBeanUtils;
import cn.xyz.repository.mongo.MongoRepository;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.stereotype.Service;

/**
 * @author xie yuan yang
 * @Date Created in 2019/9/17 19:58
 * @description TODO
 * @modified By:
 */
@Service
public class OfficialInfoCheckImpl extends MongoRepository<OfficialInfo, ObjectId> {
    @Override
    public Datastore getDatastore() {
        return SKBeanUtils.getDatastore();
    }

    @Override
    public Class<OfficialInfo> getEntityClass() {
        return OfficialInfo.class;
    }

    /**
     * @author xie yuan yang
     * @date 2019/9/17 20:01
     *      查询全部审核记录
     */
    public PageResult<OfficialInfo> getOfficialInfoList(int pageIndex, int pageSize){
        Query<OfficialInfo> query=getDatastore().createQuery(OfficialInfo.class);
        PageResult<OfficialInfo> data=new PageResult<>();
        data.setCount(query.count());
        data.setData(query.asList(pageFindOption(pageIndex, pageSize, 1)));
        return data;
    }

    /**
     * @author xie yuan yang
     * @date 2019/9/18 9:34
     *          删除审核纪律
     */
    public void delOfficialInfoLog(ObjectId id){
        Query<OfficialInfo> query=getDatastore().createQuery(OfficialInfo.class).field("_id").equal(id);
        getDatastore().delete(query);
    }

    /**
     * @author xie yuan yang
     * @date 2019/9/18 10:11
     *          根据id 获取审核详情
     */
    public OfficialInfo getOfficialInfo(ObjectId id){
        Query<OfficialInfo> query=getDatastore().createQuery(OfficialInfo.class).field("_id").equal(id);
        return query.get();
    }

    /**
     * @author xie yuan yang
     * @date 2019/9/19 11:35
     *      根据电话号码查出公众号信息
     */
    public  OfficialInfo getOfficialInfo(String telephone){
        Query<OfficialInfo> query=getDatastore().createQuery(OfficialInfo.class).field("telephone").equal(telephone);
        return query.get();
    }

    /**
     * @author xie yuan yang
     * @date 2019/9/18 12:11
     *          确认审核
     */
    public OfficialInfo updateOfficialInfo(ObjectId id,int verify,String feedback){
        Query<OfficialInfo> q = getDatastore().createQuery(OfficialInfo.class).field("_id")
                .equal(id);
        UpdateOperations<OfficialInfo> ops = getDatastore().createUpdateOperations(getEntityClass());
        ops.set("verify",verify);
        ops.set("feedback",feedback);
        return getDatastore().findAndModify(q,ops);
    }

    /**
     * @author xie yuan yang
     * @date 2019/9/19 18:35
     *      重新审核
     */
    public OfficialInfo updateOfficialInfoByTel(OfficialInfo info){
        Query<OfficialInfo> q = getDatastore().createQuery(OfficialInfo.class).field("telephone")
                .equal(info.getTelephone());
        UpdateOperations<OfficialInfo> ops = getDatastore().createUpdateOperations(getEntityClass());
        ops.set("verify",0);
        ops.set("feedback","");
        ops.set("adminID",info.getFeedback());
        ops.set("adminName",info.getAdminName());
        ops.set("adminTelephone",info.getAdminTelephone());
        ops.set("companyBusinessLicense",info.getCompanyBusinessLicense());
        ops.set("companyName",info.getCompanyName());
        ops.set("companyType",info.getCompanyType());
        ops.set("desc",info.getDesc());

        return getDatastore().findAndModify(q,ops);
    }
}
